package com.guoxin.mashibing.concurrent;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.CountDownLatch;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author guoxin
 * @date 2019/7/11
 */
public class ConcurrentMap {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
//        Map<String, String> map = new HashMap<>();
//        Map<String, String> map = new Hashtable<>();
        Map<String, String> map = new ConcurrentSkipListMap<>();
//        Map<String, String> map = new ConcurrentHashMap<>();
        Random r = new Random();
        Thread[] ths = new Thread[100];

        IntStream.range(0, ths.length).forEach(i -> ths[i] = new Thread(() -> IntStream.range(0, 10000).forEach(j -> {
            map.put("a" + r.nextInt(100000), "a" + r.nextInt(100000));
        })));

        Stream.of(ths).forEach(Thread::start);
        Stream.of(ths).forEach(thread -> {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }
}
